﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Linq;
using Hxsoft.AppV1.Module;

namespace Hxsoft.AppV1._Controls
{
	[ValidationProperty("SelectedValue")]
	public partial class MultSiteDicSelector : System.Web.UI.UserControl
	{
		public bool Enabled
		{
			get { return CtrSelector.Enabled; }
			set { CtrSelector.Enabled = value; }
		}

		private LoadedValue<int[]> loadedSiteIds;
		public int[] SiteIds
		{
			get
			{
				if (loadedSiteIds.Loader == null)
					loadedSiteIds.Loader = () => (int[])ViewState["SiteIds"];
				return loadedSiteIds.Value;
			}
			set
			{
				if (loadedSiteIds.Setter == null)
					loadedSiteIds.Setter = v => ViewState["SiteIds"] = v;
				loadedSiteIds.Value = value;
			}
		}

		public string StringSiteIds
		{
			get
			{
				return Sites.BuildOwnedSiteIds(SiteIds);
			}
			set
			{
				SiteIds = Sites.DecompoundOwnedSiteIds(value);
			}
		}

		private LoadedValue<string> loadedClassCode;
		public string ClassCode
		{
			get
			{
				if (loadedClassCode.Loader == null)
					loadedClassCode.Loader = () => (string)ViewState["ClassCode"];
				return loadedClassCode.Value;
			}
			set
			{
				if (loadedClassCode.Setter == null)
					loadedClassCode.Setter = v => ViewState["ClassCode"] = v;
				loadedClassCode.Value = value;
			}
		}

		private LoadedValue<DicValueType> loadedValueType;
		public DicValueType ValueType
		{
			get
			{
				if (loadedValueType.Loader == null)
					loadedValueType.Loader = () => EnumExtensions.ConvertFromOrDefault<DicValueType>(ViewState["ValueType"], DicValueType.DicId);
				return loadedValueType.Value;
			}
			set
			{
				if (loadedValueType.Setter == null)
					loadedValueType.Setter = v => ViewState["ValueType"] = v;
				loadedValueType.Value = value;
			}
		}

		public string SelectedValue
		{
			get { return CtrSelector.SelectedValue; }
			set
			{
				CtrSelector.DataBind();
				if (CtrSelector.Items.FindByValue(value) != null)
					CtrSelector.SelectedValue = value;
			}
		}


		private bool isBindCtrSelector = false;
		protected void CtrSelector_DataBinding(object sender, EventArgs e)
		{
			if (isBindCtrSelector) return;

			List<Hxsoft.AppV1.Module.Site> sites = null;
			if (SiteIds == null || SiteIds.Length == 0)
				sites = Sites.GetItems();
			else
				sites = Sites.GetItems(SiteIds);
			foreach (var site in sites)
			{
				ListItem listItem = new ListItem(site.SiteName, "");
				listItem.Attributes["style"] = "background-color:#ccc;";
				CtrSelector.Items.Add(listItem);

				int? classId = DicClasses.GetIdentifier(site.SiteId, ClassCode);
				if (classId.HasValue)
				{
					List<Dictionary> dics = Dictionaries.GetItems(classId.Value, null);
					foreach (var dic in dics)
						CtrSelector.Items.Add(new ListItem("　　" + dic.Name, ValueType == DicValueType.Value ? dic.Value.ToString() : dic.DicId.ToString()));
				}
			}

			CtrSelector.Attributes["onchange"] = "if(!this.value) {alert('请选择具体的项目。'); this.selectedIndex = 0;}";
		}

		protected void CtrSelector_DataBound(object sender, EventArgs e)
		{
			CtrSelector.EnsurePrompt();

			CtrSelector.DataSource = null;
			isBindCtrSelector = true;
		}
	}
}